package commonClass;

import java.util.ArrayList;
import org.json.JSONException;
import org.json.JSONObject;
import Modle.disease.Symptoms;
import android.util.Log;

public class QueryAllbaseSymptomsByAnimalIDAction implements IAnimalDoctorAction {

	
	@Override	
	public String object2json(ArrayList<Object> list) {
		
		String jsonString="";
		jsonString = "animalID=" + (String) list.get(0);
//	
//		Integer j = (Integer) list.get(0);
		
		return jsonString;
	}

	@Override
	public ArrayList<Object>  json2object(String json) {

		ArrayList<Object> list = new ArrayList<Object>();
		
		if (!jsonIsData(json))
			return null;
		
		list = getListFromJson(json);

//		Integer i = new Integer(10);
//		list.add(i);
		
		return list;
	}
	private ArrayList<Object> getListFromJson(String json) {
		ArrayList<Object> list = new ArrayList<Object>();

		org.json.JSONArray jsonObjs = null;
		try {
			jsonObjs = new JSONObject(json).getJSONArray("symptoms");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String segJson = "";
		for (int i = 0; i < jsonObjs.length(); i++) {
			JSONObject jsonObj = ((JSONObject) jsonObjs.opt(i));
			Symptoms obj = json2SymptomsCategorie1(jsonObj);
			
			list.add(obj);
		}

		return list;
	}
	
	private boolean jsonIsData(String json) {
		
		JSONObject jsonObj;
		String result = "";
		try {
			jsonObj = new JSONObject(json);
			result = jsonObj.getString("code");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return (result.equals("success"));
	}
	private Symptoms json2SymptomsCategorie1(JSONObject jsonObj) {
		Symptoms obj = new Symptoms();

		// new Gson().fromJson(segJson,SymptomsCategorie.class);

		try {
		
		
			obj.setSymptomsID(jsonObj.getString("symptomsID"));
			obj.setSymptomsCategorieID(jsonObj.getString("symptomsCategorieID"));
			obj.setSymptomsCategorieName(jsonObj.getString("symptomsCategorieName"));
			obj.setSymptomsName(jsonObj.getString("symptomsName"));
/*			obj.setAnimalID(jsonObj.getString("animalID"));
			obj.setAnimalName(jsonObj.getString("animalName"));*/
			
/*			obj.setAnimalDiseaseCategorieID(jsonObj.getString("animalDiseaseCategorieID"));

			obj.setAnimalDiseaseCategorieName(jsonObj.getString("animalDiseaseCategorieName"));*/

			Log.v("000000", "SymptomsCategorie:" + obj.toString());
			return obj;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
}